package com.example.api.service.impl;

import com.example.api.dao.VehicleDao;
import com.example.api.model.constants.LogisticsConstants;
import com.example.api.model.entity.Vehicle;
import com.example.api.model.support.RespBean;
import com.example.api.service.VehicleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class VehicleServiceImpl implements VehicleService {

    @Resource
    private VehicleDao vehicleDao;

    @Override
    public RespBean save(Vehicle vehicle) {
        Vehicle oldVehicle = vehicleDao.selectByNumberAndType(vehicle.getNumber(), vehicle.getType());
        if (Objects.nonNull(oldVehicle)) {
            return RespBean.error("车牌为" + vehicle.getNumber() + "的" + vehicle.getType() + "已存在，添加失败");
        }
        vehicle.setDriving(LogisticsConstants.VEHICLE_DRIVING_1);
        vehicle.setCreateTime(new Date());
        if (vehicleDao.insertSelective(vehicle) > 0) {
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @Override
    public RespBean update(Vehicle vehicle) {
        if (vehicleDao.updateByPrimaryKeySelective(vehicle) > 0) {
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @Override
    public RespBean delete(Integer id) {
        if (vehicleDao.deleteByPrimaryKey(id) > 0) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @Override
    public Vehicle findById(Integer id) {
        return vehicleDao.selectByPrimaryKey(id);
    }

    @Override
    public List<Vehicle> findAll() {
        return vehicleDao.findAll();
    }

}